<?php

/*
	Class: Dealers_IndexController

	About: Author
		Rich Joslin

	About: License
		<http://communit.as/docs/license>
*/
class Dealers_IndexController extends Cts_Controller_Action_Abstract {

	/* Group: Instance Methods */

	function init(){
		parent::init();
	}

	/* Group: Actions */

	/*
		Function: index
	*/
	function indexAction() {		
		
		$request = new Cts_Request($this->getRequest());
		
		$countries_table = new Countries();
		$dealer_categories_table = new DealersCategories();
		$dealer_categories = $dealer_categories_table->fetchAll();
		$dc_array = array();
		if(count($dealer_categories) > 0){
			foreach($dealer_categories as $category){
				$dc_array[] = array("id" => $category->id,"name" => $category->name);
			}
		}
		$this->view->geartypes = $dc_array;
		$this->view->countries = $countries_table->getCountriesArray("Select Country");
		$this->view->states = Cts_Common::getUsStatesArray("Select State");

		$city = $request->dealer_city;  
		$state = $request->dealer_state;  
		$country = $request->dealer_country_code;  
		$this->view->city = $city;
		$this->view->state = $state;
		$this->view->country = $country;
			
			
		// $limit = Cts_Registry::get('online_dealer_limit','dealers');	
								
		$online_dealers_table = new DealersViewDealers();
		$online_dealers = $online_dealers_table->fetchAllArray($online_dealers_table->select()
		->where("dealer_type_id = ?", 2)
		->where("status = ?", "active")
		// ->where("referral_percentage != ?", 0)
		->order("name asc")
		);		

		$this->view->online_dealers = $online_dealers;
		
		

	}
	
	function resultsAction(){
		
		if($this->getRequest()->isPost()){
			$request = new Cts_Request($this->getRequest());
			$geartypes = null;
			$limit = Cts_Registry::get("frontend_dealer_limit","dealers");
			if($request->has('limit')){				
				if($request->limit < $limit){
					$limit = $request->limit;
				}				
			}
			$dealer_categories_table = new DealersCategories();
			$dealers_dealers_categories_table = new DealersDealersCategories();
			$dealer_categories = $dealer_categories_table->fetchAll();
			$dealer_cats_array = array();
			if(count($dealer_categories) > 0){
				foreach($dealer_categories as $dc){
					$dealer_cats_array[$dc->id] = $dc->name;
				}				
			}
			
			if($request->has('geartypes')){								
				if(is_array($request->geartypes)){
					$geartypes = $request->geartypes;
				}
			}
			
			$lat = $request->lat;  // latitude of centre of bounding circle in degrees
			$lon = $request->lng;  // longitude of centre of bounding circle in degrees
			$rad = Cts_Registry::get("frontend_dealer_km_radius","dealers");  // radius of bounding circle in kilometers
		
			$dealers_table = new DealersViewDealers();
			$dealers = $dealers_table->getWithinRadius($lat,$lon,$rad,$limit,0,$geartypes);
			//dd($dealers);
			$chr = 65;
			if(count($dealers) > 0){
				$tmp_dealers = array();			
				foreach($dealers as $index => $dealer){		
					$categories = $dealers_dealers_categories_table->fetchAll($dealers_dealers_categories_table->select()->where("dealer_id = ?",$dealer['id']));
											
					if(count($categories) > 0){
						$dgt = "";
						foreach($categories as $cindex => $category){
							if($cindex > 0){
								$dgt .= " ,";
							}
							$dgt .= $dealer_cats_array[$category->dealer_category_id] . " ";
											
						}			
					}
					$dealer['geartypes'] = $dgt;
					$dealer['letter'] = chr($chr + $index);	
					$dealer['D'] = round($dealer['D'],2);
					$tmp_dealers[] = $dealer;
				}

				$this->view->dealers = $tmp_dealers;

			}
		
		} else {
			// this should never be called directly, so if it is, die.
			die();
		}
		
		
	}

	function quicklistAction()
	{
		if($this->getRequest()->isPost()){
			$request = new Cts_Request($this->getRequest());
			$geartypes = null;
			$limit = Cts_Registry::get("frontend_dealer_limit","dealers");
			if($request->has('limit')){				
				if($request->limit < $limit){
					$limit = $request->limit;
				}				
			}
			$dealer_categories_table = new DealersCategories();
			$dealers_dealers_categories_table = new DealersDealersCategories();
			$dealer_categories = $dealer_categories_table->fetchAll();
			$dealer_cats_array = array();
			if(count($dealer_categories) > 0){
				foreach($dealer_categories as $dc){
					$dealer_cats_array[$dc->id] = $dc->name;
				}				
			}
			
			if($request->has('geartypes')){								
				if(is_array($request->geartypes)){
					$geartypes = $request->geartypes;
				}
			}
			
			$lat = $request->lat;  // latitude of centre of bounding circle in degrees
			$lon = $request->lng;  // longitude of centre of bounding circle in degrees
			$rad = Cts_Registry::get("frontend_dealer_km_radius","dealers");  // radius of bounding circle in kilometers
		
			$dealers_table = new DealersViewDealers();
			$dealers = $dealers_table->getWithinRadius($lat,$lon,$rad,$limit,0,$geartypes);
			//dd($dealers);
			$chr = 65;
			if(count($dealers) > 0){
				$tmp_dealers = array();			
				foreach($dealers as $index => $dealer){		
					$categories = $dealers_dealers_categories_table->fetchAll($dealers_dealers_categories_table->select()->where("dealer_id = ?",$dealer['id']));
											
					if(count($categories) > 0){
						$dgt = "";
						foreach($categories as $cindex => $category){
							if($cindex > 0){
								$dgt .= " ,";
							}
							$dgt .= $dealer_cats_array[$category->dealer_category_id] . " ";
											
						}			
					}
					$dealer['geartypes'] = $dgt;
					$dealer['letter'] = chr($chr + $index);	
					$dealer['D'] = round($dealer['D'],2);
					$tmp_dealers[] = $dealer;
				}

				$this->view->dealers = $tmp_dealers;

			}
		
		} else {
			// this should never be called directly, so if it is, die.
			die();
		}
	}


}
